package com.xd.lib.listview.horizontal;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xd.lib.R;

import java.util.List;
import java.util.Map;

/**
 * 自定义Adapter，填充HvListView，并实现隔行变色的功能
 * Created by Administrator on 2015/6/5.
 */
public class HvListViewAdapter extends BaseAdapter {

    private List<? extends Map<String, ?>> data = null;
    private Context context = null;
    private int resource = 0;
    private String[] from = null;
    private int[] to = null;
    private HvListView listView = null;

    public HvListViewAdapter(Context context,
                             List<? extends Map<String, ?>> data,
                             int resource,
                             String[] from,
                             int[] to,
                             HvListView listView){
        this.context = context;
        this.data = data;
        this.resource = resource;
        this.from = from;
        this.to = to;
        this.listView = listView;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(resource, null);
        }

        for (int i = 0; i < to.length; i++) {
            ((TextView) convertView.findViewById(to[i])).setText(data.get(position).get(from[i]).toString());
        }

        //校正（处理同时上下和左右滚动出现错位情况）
        View child = ((ViewGroup) convertView).getChildAt(1);
        int head = listView.getHeadScrollX();
        if (child.getScrollX() != head) {
            child.scrollTo(listView.getHeadScrollX(), 0);
        }

        //隔行变色
        if (position % 2 == 0){
            convertView.setBackgroundResource(R.color.ivory);
        }else{
            convertView.setBackgroundResource(R.color.peachpuff);
        }

        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

